package cn.guetzjb.sys.controller;

import cn.guetzjb.core.entity.R;
import cn.guetzjb.core.utils.IpUtils;
import cn.guetzjb.security.anotation.InnerAuth;
import cn.guetzjb.security.anotation.RequiresPermissions;
import cn.guetzjb.sys.domain.SysLogininfor;
import cn.guetzjb.sys.domain.SysOperLog;
import cn.guetzjb.sys.domain.SysUser;
import cn.guetzjb.sys.service.SysLogininforService;
import cn.guetzjb.sys.service.SysUserService;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/logininfor")
@RequiredArgsConstructor
@Tag(name = "登录日志类")
@Slf4j
public class LogininforController {

    private final SysLogininforService logininforService;
    private final SysUserService userService;

    @Operation(summary = "添加登录日志(成功)")
    @PostMapping("/addLogSuccess")
    public boolean addLogSuccess(@RequestBody SysLogininfor sysLogininfor) {
        sysLogininfor.setIpaddr(IpUtils.getIpAddr());
        sysLogininfor.setStatus("0");
        sysLogininfor.setAccessTime(new Date());
        return logininforService.save(sysLogininfor);
    }

    @Operation(summary = "添加登录日志(错误)")
    @PostMapping("/addLogError")
    public boolean addLogError(@RequestBody SysLogininfor sysLogininfor) {
        sysLogininfor.setIpaddr(IpUtils.getIpAddr());
        sysLogininfor.setStatus("1");
        sysLogininfor.setAccessTime(new Date());
        return logininforService.save(sysLogininfor);
    }

    @Operation(summary = "更新用户登录信息(inner)", description = "内部调用")
    @PostMapping("/addUserLoginInfo")
    @InnerAuth
    public boolean addUserLoginInfo(@RequestBody SysUser sysUser) {
        sysUser.setLoginIp(IpUtils.getIpAddr());
        sysUser.setLoginDate(new Date());
        return userService.updateById(sysUser);
    }

    @Operation(summary = "分页查询")
    @GetMapping("/page")
    @RequiresPermissions(value = "log:query")
    public R getPage(@RequestParam(required = false, defaultValue = "1") Integer current,
                     @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                     @RequestParam(required = false) String params) {
        Page<SysLogininfor> page = logininforService.page(new Page<>(current, pageSize), Wrappers.<SysLogininfor>lambdaQuery()
                .like(!StringUtils.isEmpty(params), SysLogininfor::getUserName, params)
                .or()
                .like(!StringUtils.isEmpty(params), SysLogininfor::getMsg, params)
                .or()
                .like(!StringUtils.isEmpty(params), SysLogininfor::getIpaddr, params)
                .orderByDesc(SysLogininfor::getAccessTime));
        return R.ok(page);
    }

    @Operation(summary = "修改")
    @PostMapping("/update")
    @RequiresPermissions(value = "log:update")
    public R updateLog(@RequestBody SysLogininfor sysLogininfor) {
        boolean ok = logininforService.updateById(sysLogininfor);
        if (ok) {
            return R.ok();
        }
        return R.failed("更新异常");
    }

    @Operation(summary = "删除")
    @PostMapping("/delete/{ids}")
    @RequiresPermissions(value = "log:delete")
    public R deleteLog(@PathVariable List<Integer> ids) {
        boolean ok = logininforService.removeBatchByIds(ids);
        if (ok) {
            return R.ok();
        }
        return R.failed("删除异常");
    }
}
